﻿using Microsoft.AspNetCore.Components;
using MXERP.Infrastructure.Managers;

namespace MXERP.Client.Pages
{
    public partial class RoleForm
    {
        [CascadingParameter] protected IErrorHandler? ErrorHandler { get; set; }
        [Inject]
        public IRoleManager RoleManager { get; set; } = default!;
        public MudForm Form { get; set; } = default!;
        public string Title { get; set; } = "添加角色";
        public RoleResponse Model { get; set; } = new RoleResponse();
        public RoleFluentValidator Validator { get; set; } = new RoleFluentValidator();
        [Parameter]
        public long? Id { get; set; }

        private async Task Detail(long id)
        {
            var result = await RoleManager.Detail(0);
            Model = result.Data;
        }

        private async Task Save()
        {
            await Form.Validate();
            if (Form.IsValid)
            {
                await RoleManager.Save(Model);
                Snackbar.Add("保存成功!");
            }
        }

        protected override async Task OnInitializedAsync()
        {
            if (Id != null)
            {
                Title = "修改角色";
                await Detail(Id.Value);
            }
        }
    }
}
